package com.eks.shiro.controller;

import com.eks.shiro.domain.User;
import com.eks.shiro.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * LoginController
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2021-03-17 16:14
 */
@RestController
public class LoginController {

    @PostMapping("/login")
    public String login(@RequestBody User user) {
        if (Objects.isNull(user.getUname())) {
            return "用户名不能为空";
        }
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUname(), "123456");
        SecurityUtils.getSubject().login(token);
        return (String) SecurityUtils.getSubject().getSession().getId();
    }

    @PostMapping("/logout")
    public String logout(){
        SecurityUtils.getSubject().logout();
        return "登出成功";
    }
}
